// pages/personalpage/personalpage.js
const db = wx.cloud.database()
const user = db.collection('user')
const club = db.collection('club')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: './user-unlogin.png',
    userInfo: {},
    logged: false,
    takeSession: false,
    requestResult: '',
    myclubmanage:'',
    ifmaster: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
            }
          })
        }
      }
    })
    user.get().then(res=>{
      this.setData({
        ifmaster: res.data[0].ifmaster,
        myclubmanage: res.data[0].clubManage
      })
    })
  },

  onGetUserInfo: function (e) {
    // const app = getApp()
    var _this = this
    if (!this.data.logged && e.detail.userInfo) {
      this.setData({
        logged: true,
        avatarUrl: e.detail.userInfo.avatarUrl,
        userInfo: e.detail.userInfo
      },()=>{
        user.get().then(res=>{
          if(res.data.length==0){
            var extraUserInfo = {}
            extraUserInfo.username = null
            extraUserInfo.userphone = null
            extraUserInfo.userstuid = null
            user.add({
              data: {
                userInfo: e.detail.userInfo,
                extraUserInfo: extraUserInfo,
                ifmaster: false,
                clubManage: ''
              }
            })
          }
        })
      })
    }
    
  },

  onClickMyClub: function (e){
    wx.navigateTo({
      url: '../myclublist/myclublist'
    })
  },

  onClickMyInfo: function(e){
    wx.navigateTo({
      url: '../editinfo/editinfo'
    })
  },

  onClickMyEvent: function (e) {
    wx.navigateTo({
      url: '../myeventlist/myeventlist'
    })
  },

  onClickMyManageClub: function (e) {
    var mymanageclubid=[]
    user.get().then(res=>{
      mymanageclubid = res.data[0].clubManage
      wx.navigateTo({
        url: '../myclubmanage/myclubmanage?mymanageclubid='+mymanageclubid
      })
    })
  },

  onClickManage: function (e) {
    var id='';
    club.get().then(function (res) {
      id = res.data[0]._id
      console.log(id)
      wx.navigateTo({
        url: '../clubmanage/clubmanage?id=' + id
      })
    })
  },

  onWaiting:function(e){
    wx.navigateTo({
      url: '../myeventlist/myeventlist?active='+1
    })
  },

  onAll: function (e) {
    wx.navigateTo({
      url: '../myeventlist/myeventlist'
    })
  },

  onUnvalue: function (e) {
    wx.navigateTo({
      url: '../myeventlist/myeventlist?active=' + 2
    })
  },

  onFinish: function (e) {
    wx.navigateTo({
      url: '../myeventlist/myeventlist?active=' + 3
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})